package com.situ.home;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.student.entity.Areas;
import com.situ.student.entity.City;
import com.situ.student.entity.Province;
import com.situ.student.util.JDBCUtil;

import net.sf.json.JSONArray;

public class SelectAreaServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectProvince":
			selectProvince(resp);
			break;
		case "selectCity":
			selectCity(req, resp);
			break;
		case "selectArea":
			selectArea(req, resp);
			break;
		default:
			break;
		}
	}

	public void selectArea(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String cityId = req.getParameter("cityId");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Areas> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id ,area from tm_area where city_id=?";
			// 预编译
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(cityId));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String area = resultSet.getString("area");
				Areas ar = new Areas(id, area);
				list.add(ar);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		for (Areas areas : list) {
			System.out.println(areas);
		}

		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}

	public void selectCity(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String provinceId = req.getParameter("provinceId");
		System.out.println(provinceId);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<City> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,city from tm_city where province_id=? ";
			// 预编译
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Integer.parseInt(provinceId));
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String city = resultSet.getString("city");
				City ci = new City(id, city);
				list.add(ci);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		for (City city : list) {
			System.out.println(city);
		}

		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}

	public void selectProvince(HttpServletResponse resp) throws IOException {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Province> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select * from tm_province";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String province = resultSet.getString("province");
				Integer sort = resultSet.getInt("sort");
				Province pro = new Province(id, province, sort);
				list.add(pro);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		// 打印province
		for (Province province : list) {
			System.out.println(province);
		}
		
		// 转换成json数组
		JSONArray jsonArray = JSONArray.fromObject(list);
		// 解决乱码问题
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}
}
